package com.dukun.es.config;

import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
import org.springframework.data.elasticsearch.repository.config.EnableReactiveElasticsearchRepositories;

import java.time.Duration;

/**
 * Author：DK
 * Description：Elasticsearch配置类
 */
@Configuration
@EnableElasticsearchRepositories(basePackages = "com.dukun.es.repository")
public class ElasticsearchConfig {

    /**
     * 配置RestHighLevelClient
     *
     * @return RestHighLevelClient对象
     */
    @Bean
    public RestHighLevelClient restHighLevelClient() {
        ClientConfiguration clientConfiguration = ClientConfiguration.builder()
                // 连接地址
                .connectedTo("106.14.141.136:9200")
                // 连接超时间（连接到ES的时候的时间）
                .withConnectTimeout(Duration.ofSeconds(2))
                // 与ES通信的时候的超时时间
                .withSocketTimeout(Duration.ofSeconds(2))
                .build();
        return RestClients.create(clientConfiguration).rest();
    }


}
